package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.InteractionsMapper;
import com.ruoyi.system.service.IInteractionsService;

/**
 * 交互数据Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-27
 */
@Service
public class InteractionsServiceImpl implements IInteractionsService 
{
    @Autowired
    private InteractionsMapper interactionsMapper;

    /**
     * 获取今日统计数据
     * 
     * @return 统计数据
     */
    @Override
    public Map<String, Object> getTodayStats()
    {
        Map<String, Object> stats = new HashMap<>();
        
        // 获取今日日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());
        
        try {
            // 今日点赞数
            Integer todayLikes = interactionsMapper.getTodayLikes(today);
            stats.put("likes", todayLikes != null ? todayLikes : 0);
            
            // 今日评论数
            Integer todayComments = interactionsMapper.getTodayComments(today);
            stats.put("comments", todayComments != null ? todayComments : 0);
            
            // 今日收藏数
            Integer todayCollects = interactionsMapper.getTodayCollects(today);
            stats.put("collects", todayCollects != null ? todayCollects : 0);
            
            // 今日关注数
            Integer todayFollows = interactionsMapper.getTodayFollows(today);
            stats.put("follows", todayFollows != null ? todayFollows : 0);
        } catch (Exception e) {
            // 如果查询失败，返回默认值
            stats.put("likes", 0);
            stats.put("comments", 0);
            stats.put("collects", 0);
            stats.put("follows", 0);
        }
        
        return stats;
    }

    /**
     * 获取最新点赞数据
     * 
     * @return 点赞数据列表
     */
    @Override
    public List<Map<String, Object>> getRecentLikes()
    {
        try {
            return interactionsMapper.getRecentLikes();
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    /**
     * 获取最新收藏数据
     * 
     * @return 收藏数据列表
     */
    @Override
    public List<Map<String, Object>> getRecentCollects()
    {
        try {
            return interactionsMapper.getRecentCollects();
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    /**
     * 获取最新关注数据
     * 
     * @return 关注数据列表
     */
    @Override
    public List<Map<String, Object>> getRecentFollows()
    {
        try {
            return interactionsMapper.getRecentFollows();
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    /**
     * 获取最新评论数据
     * 
     * @return 评论数据列表
     */
    @Override
    public List<Map<String, Object>> getRecentComments()
    {
        try {
            return interactionsMapper.getRecentComments();
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    /**
     * 禁用评论
     * 
     * @param commentId 评论ID
     * @return 结果
     */
    @Override
    public int disableComment(Long commentId)
    {
        try {
            return interactionsMapper.updateCommentStatus(commentId, 0);
        } catch (Exception e) {
            return 0;
        }
    }

    /**
     * 启用评论
     * 
     * @param commentId 评论ID
     * @return 结果
     */
    @Override
    public int enableComment(Long commentId)
    {
        try {
            return interactionsMapper.updateCommentStatus(commentId, 1);
        } catch (Exception e) {
            return 0;
        }
    }
}
